/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mp.weixin.WXpublic.send;

import java.util.List;
import mp.weixin.WXpublic.BaseReceiveMessage;
import mp.weixin.WXpublic.BaseSendMessage;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 *
 * @author 王仲秋
 */
public class SNewsEntity extends BaseSendMessage {

    private int articleCount;
    private List<WeixinArticle> articles;

    public SNewsEntity() {

    }

    public SNewsEntity(BaseReceiveMessage brm, int articleCount, List<WeixinArticle> articles) {
        super(brm);
        this.articleCount = articleCount;
        this.articles = articles;
    }

    public int getArticleCount() {
        return articleCount;
    }

    public void setArticleCount(int articleCount) {
        this.articleCount = articleCount;
    }

    public List<WeixinArticle> getArticles() {
        return articles;
    }

    public void setArticles(List<WeixinArticle> articles) {
        this.articles = articles;
    }

    @Override
    public SendTypeEnum getMsgType() {
        return SendTypeEnum.NEWS;
    }

    @Override
    public String toWeiXinMessageXMLString() {
        /**
         * <xml>
         * <ToUserName><![CDATA[toUser]]></ToUserName>
         * <FromUserName><![CDATA[fromUser]]></FromUserName>
         * <CreateTime>12345678</CreateTime>
         * <MsgType><![CDATA[news]]></MsgType>
         * <ArticleCount>2</ArticleCount>
         * <Articles>
         * <item>
         * <Title><![CDATA[title1]]></Title>
         * <Description><![CDATA[description1]]></Description>
         * <PicUrl><![CDATA[picurl]]></PicUrl>
         * <Url><![CDATA[url]]></Url>
         * </item>
         * <item>
         * <Title><![CDATA[title]]></Title>
         * <Description><![CDATA[description]]></Description>
         * <PicUrl><![CDATA[picurl]]></PicUrl>
         * <Url><![CDATA[url]]></Url>
         * </item>
         * </Articles>
         * </xml>
         */
//    	 StringBuilder xmlStr = new StringBuilder();
//         xmlStr.append("<xml>");
//         xmlStr.append("<ToUserName><![CDATA[").append(this.getToUserName()).append("]]></ToUserName>");
//         xmlStr.append("<FromUserName><![CDATA[").append(this.getFromUserName()).append("]]></FromUserName>");
//         xmlStr.append("<CreateTime>").append(this.getCreateTime().getTime()).append("</CreateTime>");
//         xmlStr.append("<MsgType><![CDATA[").append(this.getMsgType().getType()).append("]]></MsgType>");
//         xmlStr.append("<ArticleCount><![CDATA[").append(this.getArticleCount()).append("]]></ArticleCount>");
//         xmlStr.append("<Articles>");
//         	for(int i = 0; i<this.articleCount; i++ ){
//         		WeixinArticle article = this.articles.get(i);
//         		xmlStr.append("<item>");
//         		xmlStr.append("<Title><![CDATA[").append(article.getTitle()).append("]]></Title>");
//         		xmlStr.append("<Description><![CDATA[").append(article.getDescription()).append("]]></Description>");
//         		xmlStr.append("<PicUrl><![CDATA[").append(article.getPicUrl()).append("]]></PicUrl>");
//         		xmlStr.append("<Url><![CDATA[").append(article.getUrl()).append("]]></Url>");
//         		xmlStr.append("</item>");
//         	}
//         xmlStr.append("</Articles>");
//         xmlStr.append("</xml>");
//         return xmlStr.toString();
        String returnStr = "";
        Document doc = DocumentHelper.createDocument();
        Element rootXML = doc.addElement("xml");
        rootXML.addElement("ToUserName").setText(this.getToUserName());
        rootXML.addElement("FromUserName").setText(this.getFromUserName());
        rootXML.addElement("CreateTime").setText(this.getCreateTime().getTime() + "");
        rootXML.addElement("MsgType").setText(this.getMsgType().getType());
        rootXML.addElement("ArticleCount").setText(this.getArticleCount() + "");
        Element el = rootXML.addElement("Articles");
        for (int i = 0; i < this.articleCount; i++) {
            WeixinArticle article = this.articles.get(i);
            Element el2 = el.addElement("item");
            el2.addElement("Title").setText(article.getTitle());
            el2.addElement("Description").setText(article.getDescription());
            el2.addElement("PicUrl").setText(article.getPicurl());
            el2.addElement("Url").setText(article.getUrl());
        }
        returnStr = rootXML.asXML();
        return returnStr;
    }

    public class WeixinArticle {

        private String title;
        private String description;
        private String picurl;
        private String url;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getPicurl() {
            return picurl;
        }

        public void setPicurl(String picurl) {
            this.picurl = picurl;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

    }
}
